# Maintainer: Daurnimator <daurnimator@archlinux.org>
# Maintainer: Jelle van der Waa <jelle@archlinux.org>

pkgname=('loki' 'loki-canary' 'promtail' 'logcli')
pkgver=2.8.1
pkgrel=1
pkgdesc='like Prometheus, but for logs'
url='https://github.com/grafana/loki'
arch=('loong64' 'x86_64')
license=('AGPL3' 'Apache')
depends=('glibc')
makedepends=('go' 'systemd')
options=('!lto')
source=($pkgname-$pkgver.tar.gz::https://github.com/grafana/loki/archive/v$pkgver.tar.gz
        promtail.sysusers promtail.service promtail.tmpfiles
        loki.sysusers loki.service loki.tmpfiles)
sha512sums=('6197126ed8290c27daa3ca3319160d0e838c6577f2927d290a565c48ff83d6f41a5f516b6478e5441e0f448f67cb02ef9b73ad909e640ab01a27d765e8c96184'
            '2b6c44b18ea3c9f955a7450222180d0b20b5fc551d0b7e5d0d8949e40adc847c4166829146260f87a75732cc5473eab0347dd56fc2125517698bac0652738c74'
            '18011b135c7817f2542f7a93b607ee46e5d61625921c7dc6e46e1b29e31c67147653b215d8677fde21cdce3b577c479cebd2d85f47e4b8cbe4c30f8be5f910cd'
            '598042c40673a7914c5a1eeccfb78f832379a61f4360212c5d86f667343cf2fc78e98d9025f9717ea64f3e16e0a28f08cd7709706d811656722019f6167dd788'
            'f00b3cb64b71d3ca5a422a2bdff1f81a3e2707c4f73bcb27e24338eabff039b96125c847aaae43c9b22ccc47f89585118c1d0e0b29c4eb7b6f9260c68f8a8324'
            '03904b67617e4064a1eb39b6edfaec5d55bc85f142e867d115d3d0e12494a052fddf13bb6f5c541aaca29a18582df2332bb0cc6b8f29928cf6a0d5d2710ead74'
            'de40d1e6752edbf8c21317ce1ee10f98dfc869e569c07092c613f7144e261e9438683a145a2e0e37e2a9fc758c5c2f02e1d0ac1c60347e98b147e4a550ec6040')

prepare() {
  cd "$pkgname-$pkgver"

  go mod download
}

build() {
  cd loki-$pkgver

  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPROXY=off
  for path in cmd/loki cmd/loki-canary cmd/logcli clients/cmd/promtail; do
    go build \
      -trimpath \
      -buildmode=pie \
      -mod=readonly \
      -modcacherw \
      -tags promtail_journal_enabled \
      -ldflags " \
         -X github.com/grafana/loki/pkg/util/build.Version=$pkgver
         -X github.com/grafana/loki/pkg/util/build.BuildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH})
         -linkmode external
         -extldflags \"${LDFLAGS}\"" \
      ./"$path"
  done
}

check() {
  cd loki-$pkgver

  # Tests assume UTC localtime
  # https://github.com/grafana/loki/issues/9081#issuecomment-1510308817
  TZ=UTC go test ./...
}

package_loki() {
  backup=('etc/loki/loki.yaml')

  cd loki-$pkgver

  install -Dm755 -t "$pkgdir"/usr/bin loki

  install -Dm644 cmd/loki/loki-local-config.yaml $pkgdir/etc/loki/loki.yaml

  install -Dm644 ${srcdir}/loki.sysusers "$pkgdir/usr/lib/sysusers.d/loki.conf"
  install -Dm644 ${srcdir}/loki.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/loki.conf"
  install -Dm644 ${srcdir}/loki.service "$pkgdir/usr/lib/systemd/system/loki.service"
}

package_loki-canary() {
  pkgdesc="A standalone app that audits the log capturing performance of Loki"

  cd loki-$pkgver

  install -Dm755 -t "$pkgdir"/usr/bin loki-canary
}

package_promtail() {
  pkgdesc="An agent which ships the contents of local logs to a private Loki instance or Grafana Cloud"
  backup=('etc/loki/promtail.yaml')

  cd loki-$pkgver

  install -Dm755 -t "$pkgdir"/usr/bin promtail

  install -Dm644 clients/cmd/promtail/promtail-local-config.yaml $pkgdir/etc/loki/promtail.yaml

  install -Dm644 ${srcdir}/promtail.sysusers "$pkgdir/usr/lib/sysusers.d/promtail.conf"
  install -Dm644 ${srcdir}/promtail.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/promtail.conf"
  install -Dm644 ${srcdir}/promtail.service "$pkgdir/usr/lib/systemd/system/promtail.service"
}

package_logcli() {
  pkgdesc="A command-line for loki"

  cd loki-$pkgver

  install -Dm755 -t "$pkgdir"/usr/bin logcli
}
